package think.in.java.concurrency.practise.exec8.ForBlockQueue;

import java.util.Date;
import java.util.Random;

/**
 * Created by IntelliJ IDEA.
 * User: Richard
 * Date: 6/18/11
 * Time: 12:09 AM
 * To change this template use File | Settings | File Templates.
 */
public class Chef implements Runnable {
    private static int dishes = 0;
    private Restrant restrant;
    private int chefNum = 0;
    private static Object notifier = new Object();

    public Chef(Restrant restrant, int num) {
        this.setRestrant(restrant);
        this.chefNum = num;
    }

    public static Object getNotifier() {
        return notifier;
    }

    public static void setNotifier(Object notifier) {
        Chef.notifier = notifier;
    }

    public synchronized void work() throws InterruptedException {
        if (getRestrant().getWaitingFoodCustomers().size() == 0 ) {
            synchronized (notifier){
                notifier.wait();
            }
        } else {
            cook();
        }
    }

    public void run() {
        getReady();
        try {
            while (!Thread.interrupted()) {
                work();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }

    public void cook() throws InterruptedException {
        Customer customer = getRestrant().popWaitingFoodCustomers();
        if(customer==null){
            return ;
        }
        int sleepTime = new Random().nextInt(100);
        customer.setStatus(Customer.Status.waitingForDeliver);
        Thread.sleep(sleepTime);
        System.out.println("!" + this + " start cook for " + customer + " take " + sleepTime);
        getRestrant().putFood(new Food(this, customer));
        getRestrant().putWaitingDeliverCustomers(customer);
        System.out.println("!" + this + " finish cook for " + customer + " take " + sleepTime);
        synchronized (Waiter.getNotifier()){
            Waiter.getNotifier().notifyAll();
        }

    }

    public void getReady() {

        try {
            Date date = new Date();
            System.out.println(this + "start to prepare ! at " + "datetime:" + date);

            System.out.println();
            Thread.sleep(100);
            getRestrant().getChefCountDown().countDown();
            System.out.println(this + "get ready!");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    public Restrant getRestrant() {
        return restrant;
    }

    public void setRestrant(Restrant restrant) {
        this.restrant = restrant;
    }

    public int getChefNum() {
        return chefNum;
    }

    public void setChefNum(int chefNum) {
        this.chefNum = chefNum;
    }

    public String toString() {
        return "Chef : " + this.getChefNum() + " : ";
    }
}
